package com.yanqu.road.server.cmd.children;

import com.yanqu.road.entity.child.ChildTrainResult;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.child.ChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_CHILD_TRAIN_ALL, desc = "子女培养")
public class AllChildTrainCmd extends AbstractCommand {

    private List<Integer> siteIndex;

    private List<String> childUid;

    private List<Integer> trainTimes;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChildProto.AllChildTrainReqMsg reqMsg = ChildProto.AllChildTrainReqMsg.parseFrom(packet.getBytes());
        siteIndex = reqMsg.getSiteIndexList();
        childUid = reqMsg.getChildUidList();
        trainTimes = reqMsg.getTrainTimesList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //周卡特权，子嗣一键培养
        if(!player.getModule(CardModule.class).isOneKeyTrainAllChild()) {
            //身份不足
            String[] arr = GameConfig.CHILDREN_ADVANCED_TRAIN_NEED_TITLE_LIMIT.split("\\|");
            int titleLimit = Integer.parseInt(arr[0]);
            int vipLimit = Integer.parseInt(arr[1]);
            if (player.getUserInfo().getTitleId() < titleLimit && VipMgr.getVipLv(player.getUserInfo().getVipExp()) < vipLimit) {
                ChildProto.ChildTrainRespMsg.Builder respMsg = ChildProto.ChildTrainRespMsg.newBuilder();
                respMsg.setRet(GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH);
                player.sendPacket(Protocol.U_CHILD_TRAIN, respMsg);
                return;
            }
        }
        String currentChildUid = childUid.get(0);//数组第一个就是当前界面上的子嗣
        for(int i = 0; i < siteIndex.size(); i++) {
            ChildTrainResult result = player.getModule(ChildModule.class).childTrain(siteIndex.get(i), childUid.get(i), 0, trainTimes.get(i), currentChildUid);
            if (null != result) {
                ChildProto.ChildTrainRespMsg.Builder respMsg = ChildProto.ChildTrainRespMsg.newBuilder();
                respMsg.setRet(result.getRet());
                if (result.getRet() == 0) {
                    respMsg.setEffectMammonSkill(result.getEffectMammonSkill());
                    respMsg.setEffectTimes(result.getEffectTimes());
                }
                player.sendPacket(Protocol.U_CHILD_TRAIN, respMsg);
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
